﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class ChiTietVanBan : System.Web.UI.Page
    {
        string mode = null;
        private static string _fileImageName;
        public static TextHouseBO textbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                mode = Request.QueryString["masach"];
                if (mode != null)
                {
                    TextHouseBO textbo = TextHouseBO.GetTextHouseBO(Convert.ToInt32(mode));
                    textbo.ViewCount += 1;
                    try
                    {
                        textbo.Save();
                    }
                    catch { }
                }
            }
        }

        public int getextension(string filename)
        {
            try
            {
                string formatFile = Path.GetExtension(filename);
                if (formatFile.Equals(".doc"))
                    return 1;//la file .doc
                else return 2;//la file .pdf
            }
            catch
            {
                return 3;
            }
        }

        protected void lbtupload_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                     "alert('Đăng nhập để thực hiện Upload văn bản !')", true);
            }
            else if (FileUpload_VanBan.FileName.Length > 0)
            {
                textbo = TextHouseBO.NewTextHouseBO();
                textbo.UserName = Session["UserName"].ToString();
                postDoccument();
                textbo.ViewCount = 0;
                textbo.DownloadCount = 0;
                textbo.CreatedDate = DateTime.Now;
                if (OptionBO.GetNameOptionBO("tudongduyettinvanban").OptionValue == "True")
                    textbo.IsActive = true;
                else
                    textbo.IsActive = false;
                try
                {
                    textbo.Save();
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                    "alert('Upload thành công')", true);
                }
                catch { }
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                "alert('Chọn file cần upload')", true);
            }
        }

        private void postDoccument()
        {
            try
            {
                if (FileUpload_VanBan.HasFile)
                {
                    if (FileUpload_VanBan.FileBytes.Length <= 3145728)
                    {
                        _fileImageName = FileUpload_VanBan.FileName.Replace(" ", "_");

                        string formatFile = Path.GetExtension(_fileImageName);
                        formatFile = formatFile.ToLower();
                        //kiem tra dinh dang cho phep upload
                        if (formatFile.Equals(".doc") || formatFile.Equals(".pdf") || formatFile.Equals(".DOC") || formatFile.Equals(".PDF"))
                        {
                            //kiem tra file da ton tai
                            string filePath = HttpContext.Current.Server.MapPath("TotalUpload/VanBan/" + _fileImageName);
                            if (File.Exists(filePath))
                            {
                                //neu ton tai thi doi ten
                                _fileImageName = ImageFileName(_fileImageName);
                            }
                            //thuc hien luu file
                            try
                            {
                                FileUpload_VanBan.SaveAs(Server.MapPath("TotalUpload/VanBan/" + _fileImageName));
                                try
                                {
                                    textbo.Title = txtten.Text.Trim();
                                    textbo.UrlDownload = _fileImageName;
                                }
                                catch { }

                            }
                            catch (Exception)
                            {
                                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                "alert('Có một số vấn đề xả ra trong khi lưu. Xin hay thử lại.')", true);
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                "alert('Chỉ được upload các định dạng văn bản (.doc , .pdf)')", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                "alert('Kích thước văn bản không quá 3MB')", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success",
                                                                "alert('Chưa chọn file văn bản.')", true);
                }
            }
            catch
            {

            }
        }
        private static string ImageFileName(string fileName)
        {
            Random r = new Random();
            return r.Next(0, 10000).ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + fileName;
        }

        private void deleteExitsImage(string filedelete)
        {
            try
            {
                var pathFileImage = Server.MapPath("TotalUpload/VanBan/" + filedelete);
                FileInfo fileInfo;
                if (File.Exists(pathFileImage))
                {
                    fileInfo = new FileInfo(pathFileImage);
                    fileInfo.Delete();
                }
            }
            catch
            {
            }
        }
    }
}
